import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { join } from 'path';
import { NestExpressApplication } from '@nestjs/platform-express';
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
import {Logger, ValidationPipe} from "@nestjs/common";
import {HttpExceptionFilter} from "./core/filter/http-exception/http-exception.filter";
import {TransformInterceptor} from "./core/interceptor/transform/transform.interceptor";
import {DocumentBuilder, SwaggerModule} from "@nestjs/swagger";

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  // 设置全局路由前缀
  // app.setGlobalPrefix('api');
  // 设置CORS选项
  const corsOptions: CorsOptions = {
    origin: true,
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    preflightContinue: false,
    optionsSuccessStatus: 204,
    credentials: true,
    allowedHeaders: 'Content-Type, Accept',
  };
  // 允许跨域
  app.enableCors(corsOptions);
  //注册全局错误的过滤器
  app.useGlobalFilters(new HttpExceptionFilter());
  // 注册全局拦截器
  app.useGlobalInterceptors(new TransformInterceptor());
  //配置管道验证
  app.useGlobalPipes(new ValidationPipe());
  //配置静态图片资源
  app.useStaticAssets(join(__dirname, '..', 'public'), {
    prefix: '/static/', //设置虚拟路径
  });
  // 配置日志
  const logger = new Logger();
  app.useLogger(logger);
  // 配置Swagger
  const options = new DocumentBuilder()
    .setTitle('清单API')
    .setDescription('清单API接口文档')
    .setVersion('1.0')
    .addTag('清单')
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api-docs', app, document);

  await app.listen(3000);
}
bootstrap();
